package com.sugon.controller.tool;

import com.sugon.annotation.AutoLog;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.entity.ToolsCheckPlanEntity;
import com.sugon.entity.ToolsCheckPlanItemEntity;
import com.sugon.entity.ToolsCheckPlanItemToolsEntity;
import com.sugon.entity.ToolsListEntity;
import com.sugon.modules.base.model.vo.ProcessProgressConst;
import com.sugon.modules.tools.model.vo.ToolsCheckPlanItemToolsParamVO;
import com.sugon.service.ToolsCheckPlanItemService;
import com.sugon.service.ToolsCheckPlanItemToolsService;
import com.sugon.service.ToolsCheckPlanService;
import com.sugon.service.ToolsListService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 工装周期性检测计划明细工装关联表Controller
 *
 * @author sugon
 * @date 2021-09-23 16:26:40
 */
@Controller
@RequestMapping("toolscheckplanitemtools")
public class ToolsCheckPlanItemToolsController {

    @Autowired
    private ToolsCheckPlanItemToolsService toolsCheckPlanItemToolsService;
    @Autowired
    private ToolsCheckPlanItemService toolsCheckPlanItemService;
    @Autowired
    private ToolsCheckPlanService toolsCheckPlanService;
    @Resource
    private ToolsListService toolsListService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("toolscheckplanitemtools:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ToolsCheckPlanItemToolsEntity> toolsCheckPlanItemToolsList = toolsCheckPlanItemToolsService.queryList(query);
        int total = toolsCheckPlanItemToolsService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(toolsCheckPlanItemToolsList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("toolscheckplanitemtools:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ToolsCheckPlanItemToolsEntity toolsCheckPlanItemTools = toolsCheckPlanItemToolsService.queryObject(id);

        return R.ok().put("toolsCheckPlanItemTools", toolsCheckPlanItemTools);
    }

    /**
     * 保存
     */
    @NonBusinessParamValid
    @RequestMapping("/save")
    //@RequiresPermissions("toolscheckplanitemtools:save")
    @ResponseBody
    public R save(@Validated @RequestBody ToolsCheckPlanItemToolsParamVO planItemToolsParam, BindingResult bindingResult) {
        // 校验明细是否存在
        String itemId = planItemToolsParam.getItemId();
        ToolsCheckPlanItemEntity planItem = toolsCheckPlanItemService.queryObject(itemId);
        if (planItem == null) {
            return R.error("工装周期性校验计划明细不存在，请确认！");
        }
        ToolsCheckPlanEntity checkPlan = toolsCheckPlanService.queryObject(planItem.getPlanId());
        if (checkPlan == null) {
            return R.error("非法工装校验计划ID!");
        }
        // 校验设备状态
        Integer status = checkPlan.getStatus();
        if (!ProcessProgressConst.NEW.equals(status) && !ProcessProgressConst.PASSED.equals(status)) {
            return R.error("只有处于编辑中或审核遭拒的计划才可以关联工装!");
        }
        // 保存关联工装
        ToolsCheckPlanItemToolsEntity itemTools;
        // 工装清单ID列表
        List<String> toolsListIdList = planItemToolsParam.getToolsFileIdList();
        for (String toolsListId : toolsListIdList) {
            // 校验工装是否存在
            ToolsListEntity toolsList = toolsListService.queryObject(toolsListId);
            if (toolsList == null) {
                return R.error("工装[{}]不存在，请确认！", toolsListId);
            }
            // 校验是否已添加过
            boolean isExists = toolsCheckPlanItemToolsService.checkExists(itemId, toolsList.getToolsBaseId());
            if (isExists) {
                continue;
            }
            itemTools = new ToolsCheckPlanItemToolsEntity();
            itemTools.setItemId(itemId);
            itemTools.setToolsFileId(toolsList.getToolsBaseId());
            itemTools.setStringCapacity(toolsList.getCapacity());
            toolsCheckPlanItemToolsService.save(itemTools);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("toolscheckplanitemtools:update")
    @ResponseBody
    public R update(ToolsCheckPlanItemToolsEntity toolsCheckPlanItemTools) {
        toolsCheckPlanItemToolsService.update(toolsCheckPlanItemTools);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("toolscheckplanitemtools:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        toolsCheckPlanItemToolsService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ToolsCheckPlanItemToolsEntity> list = toolsCheckPlanItemToolsService.queryList(params);

        return R.ok().put("list", list);
    }
}
